using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using THSLib;using THSERP.BaseProvider;

namespace THSERP
{
    public partial class guiPaymentDetails : THSERPScreen
    {
        private double TotalAmount;
        private double InitialAmount;
        private int CustomerID;
        public THSList<ARDocumentPaymentDetailsInfo> PaymentList = new THSList<ARDocumentPaymentDetailsInfo>();

        public guiPaymentDetails()
        {
            InitializeComponent();
        }

        public guiPaymentDetails(BaseModule module, String type, double totalAmount, double initialAmount)
        {
            InitializeComponent();

            switch (type)
            {
                case ADConfigValueUtility.cstOpenDocumentTypeSaleReceipt:
                    fld_btnSave.Visible = false;
                    fld_btnSavePrint.Visible = false;
                    break;
                case ADConfigValueUtility.cstOpenDocumentTypeLayBy:
                    fld_btnComplete.Visible = false;
                    fld_btnCompletePrint.Visible = false;
                    break;
            }

            this.TotalAmount = totalAmount;
            this.InitialAmount = initialAmount;
            this.Module = module;

            //Get receipt total
            
            fld_lblTotalAmount.Text = THSUtil.GetNumberDisplayFormat(TotalAmount);

            //Init payment method grid
            ERPModuleEntities entity = ((BaseModuleERP)Module).CurrentModuleEntity;
            PaymentList.Clear();

            //Filter payment methods which checked_Hieu.nguyen_15/4/2010
            ADConfigValuesController objADConfigValuesController = new ADConfigValuesController();
            DataSet ds = objADConfigValuesController.GetADConfigValuesByGroup(ADConfigValueUtility.ADConfigValuePaymentMethod);
            foreach (DataRow configRow in ds.Tables[0].Rows)
            {
                if (configRow["ADConfigKeyDesc"].ToString() == "1")
                {
                    ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo = new ARDocumentPaymentDetailsInfo();
                    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo = configRow["ADConfigKeyValue"].ToString();
                    objDocumentPaymentDetailsInfo.ARPaymentMethodDisplayText = configRow["ADConfigText"].ToString();
                    if (objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodAccount)
                    {
                        CustomerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(entity.MainObject, "FK_ARCustomerID"));
                        if (CustomerID == 0)
                            continue;
                    }
                    PaymentList.Add(objDocumentPaymentDetailsInfo);
                }
            }

            //foreach (DataRow configRow in ADConfigValueUtility.glbdsConfigValues.Tables[ADConfigValueUtility.ADConfigValuePaymentMethod].Rows)
            //{
            //    ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo = new ARDocumentPaymentDetailsInfo();
            //    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo = configRow["Value"].ToString();
            //    objDocumentPaymentDetailsInfo.ARPaymentMethodDisplayText = configRow["Text"].ToString();
            //    if (objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodAccount)
            //    {
            //        CustomerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(entity.MainObject, "FK_ARCustomerID"));
            //        if (CustomerID == 0)
            //            continue;
            //    }
            //    PaymentList.Add(objDocumentPaymentDetailsInfo);
            //}
            fld_dgcPayment.DataSource = PaymentList;
            ProposeRemainingAmount();

            GridView gridView = (GridView)fld_dgcPayment.MainView;
            gridView.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(GridView_FocusedRowChanged);
        }

        private void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ProposeRemainingAmount();
        }

        public void ProposeRemainingAmount()
        {
            ARDocumentPaymentDetailsInfo currentPayment = GetCurrentRowData();
            if (currentPayment != null)
            {
                double amount = 0;
                foreach (ARDocumentPaymentDetailsInfo objDocumentPaymentDetaisInfo in PaymentList)
                    if (objDocumentPaymentDetaisInfo.ARPaymentMethodCombo != currentPayment.ARPaymentMethodCombo)
                        amount += objDocumentPaymentDetaisInfo.ARDocumentPaymentDetailAmount;

                if (InitialAmount > amount)
                    currentPayment.ARDocumentPaymentDetailAmount = InitialAmount - amount;
                fld_dgcPayment.RefreshDataSource();
            }
        }

        public ARDocumentPaymentDetailsInfo GetCurrentRowData()
        {
            GridView gridView = (GridView)fld_dgcPayment.MainView;
            int index = gridView.GetDataSourceRowIndex(gridView.FocusedRowHandle);
            if (index >= 0)
                return PaymentList[index];
            return null;
        }

        private void fld_btnAcceptPayment_Click(object sender, EventArgs e)
        {
            if (fld_btnAcceptPayment.Tag.ToString() == "Accept")
            {
                double receivingAmount = 0;
                if (Modules.CustomerPayment.CustomerPaymentModule.AcceptPayment(PaymentList, CustomerID, TotalAmount, ref receivingAmount))
                {
                    fld_lblReceivingAmount.Text = THSUtil.GetNumberDisplayFormat(receivingAmount);
                    fld_lblChangeAmount.Text = THSUtil.GetNumberDisplayFormat(receivingAmount - TotalAmount);

                    //Invalidate controls
                    fld_btnAcceptPayment.Text = "Discard Payment";
                    fld_btnAcceptPayment.Tag = "Discard";
                    fld_btnComplete.Enabled = true;
                    fld_btnCompletePrint.Enabled = true;
                    fld_btnSave.Enabled = true;
                    fld_btnSavePrint.Enabled = true;
                    fld_dgcPayment.Enabled = false;
                }
            }
            else if (fld_btnAcceptPayment.Tag.ToString() == "Discard")
            {
                fld_btnAcceptPayment.Text = "Accept Payment";
                fld_btnAcceptPayment.Tag = "Accept";
                fld_btnComplete.Enabled = false;
                fld_btnCompletePrint.Enabled = false;
                fld_btnSave.Enabled = false;
                fld_btnSavePrint.Enabled = false;
                fld_dgcPayment.Enabled = true;
            }
        }

        private void fld_btnComplete_Click(object sender, EventArgs e)
        {
            this.Tag = "Complete";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void fld_btnCompletePrint_Click(object sender, EventArgs e)
        {
            this.Tag = "CompletePrint";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void fld_btnSave_Click(object sender, EventArgs e)
        {
            this.Tag = "Save";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void fld_btnSavePrint_Click(object sender, EventArgs e)
        {
            this.Tag = "SavePrint";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}